//
//  bannerView.m
//  KG
//
//  Created by Eugene Braginets on 09.04.12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "bannerView.h"
#import "DataItem.h"
#import "AppDelegate.h"

@implementation bannerView
@synthesize msgText=_msgText;
@synthesize imageView1=_imageView1;
@synthesize imageView2=_imageView2;
@synthesize timer=_timer;
@synthesize loaded=_loaded;
@synthesize shadow = _shadow;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [[NSNotificationCenter defaultCenter] addObserver:self
             selector: @selector(gotEvent)
                 name:@"switchBanner" object: nil];
        

                
        NSLog (@"set up notification center!");
    }
    return self;
}

- (void) gotEvent {
//    NSLog(@"got event inside bannerView");
    [self showImage];
}


- (void) viewDidDisappear:(BOOL)animated {
//    [self.timer invalidate];
//    NSLog(@"invalidate!");
}

- (void) viewWillAppear:(BOOL)animated {
//    NSLog(@"view will appear");
}

- (void) viewDidAppear:(BOOL)animated {
//    NSLog(@"view did appear");
}
- (void) loadImage: (DataItem*) bannerItem {
    if (bannerItem.image) {
        NSLog (@"Banner Image %@ is loaded",bannerItem.imageAddr);
        return;
    }
    
    NSLog (@"loading BannerImage %@",bannerItem.imageAddr);
    
    NSString *imageAddr=[NSString stringWithString:bannerItem.imageAddr];
    
    NSLog (@"ImageUrl: %@", imageAddr);
    NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imageAddr]];
    UIImage* image = [[UIImage alloc] initWithData:imageData];
    [bannerItem setImage:image];
    
        
}

- (void) animate {
//    if (([myAppDelegate stringIsEmpty:[[kMessagesArray objectAtIndex:[myAppDelegate countMessage]] text]]) && (![[kBannersArray objectAtIndex:[myAppDelegate countBanner]] image])) {
//        
//        NSLog(@"Tried to show BANNERVIEW. But no message or image loaded yet. Quiting");
//        return;
//    }
    
    
    CGRect frame=CGRectMake(0, 460, 320, 74);
    [self.view setFrame:frame];
    [self.view setHidden:NO];
    [UIView 
     animateWithDuration:1 
     delay:0.2 
     options:UIViewAnimationCurveEaseOut
     animations:^{
         CGRect frame=CGRectMake(0, 386, 320, 74);
         [self.view setFrame:frame];
     }
     completion:^(BOOL finished){
         
         
     }];

}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - custom image
- (void) showMessage {
    
    if ([myAppDelegate stringIsEmpty:[[kMessagesArray objectAtIndex:[myAppDelegate countMessage]] text]]) {
        
        NSLog(@"Tried to show MESSAGE. But message not loaded yet. Quiting");
        return;
    }
    
//    NSLog(@"ORDER %d!MESSAGE",[myAppDelegate order]);

    
    [self.imageView2 setHidden:NO];
    [self.shadow setHidden:NO];
    [self.imageView1 setAlpha:1];    
    
    [self.msgText setText:[[kMessagesArray objectAtIndex:[myAppDelegate countMessage]] text]];
    
    if ([[[kMessagesArray objectAtIndex:[myAppDelegate countMessage]] urlstring] length]>0) {
        [self.imageView2 setImage:[UIImage imageNamed:@"msgbacklink"]];
    }
    else {
        [self.imageView2 setImage:[UIImage imageNamed:@"msgback"]];
    }
    
    [UIView 
     animateWithDuration:1 
     delay:0.2 
     options:UIViewAnimationCurveEaseOut
     animations:^{
         [self.imageView1 setAlpha:0];
     }
     completion:^(BOOL finished){
         
         [self.imageView1 setHidden:YES];
         [self.imageView1 setAlpha:1];
     }];

}


- (void) showBanner {

    if (![[kBannersArray objectAtIndex:[myAppDelegate countBanner]] image]) {
        
        NSLog(@"Tried to show BANNER. But banner not loaded yet. Quiting");
        return;
    }
    
//    NSLog(@"ORDER %d! BANNER",[myAppDelegate order]);
    [self.shadow setHidden:NO];
    [self.imageView1 setAlpha:0];
    [self.imageView1 setHidden:NO];
    [self.imageView1 setImage:[[kBannersArray objectAtIndex:[myAppDelegate countBanner]] image]];

    [UIView 
     animateWithDuration:1 
     delay:0.2 
     options:UIViewAnimationCurveEaseOut
     animations:^{
         [self.imageView1 setAlpha:1];
     }
     completion:^(BOOL finished){
         
     }];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    UITouch *touch = [touches anyObject];
    NSString* urlString;
    if ([touch view] == self.imageView1) {
//        object=[kBannersArray objectAtIndex:[myAppDelegate countBanner]];
        urlString=[[kBannersArray objectAtIndex:[myAppDelegate countBanner]]urlstring];
    }
    if (([touch view] == self.imageView2)||
        ([touch view] == self.msgText)){
//        object=[kMessagesArray objectAtIndex:[myAppDelegate countMessage]];
        urlString=[[kMessagesArray objectAtIndex:[myAppDelegate countMessage]]urlstring];
    }
    if (urlString) {
        NSURL *url = [ [ NSURL alloc ] initWithString: urlString ];
        [[UIApplication sharedApplication] openURL:url];
    }
}

- (void) showImage {
    
//    NSLog(@"Change banner!");

    if ([myAppDelegate order]==0) {
        [self showMessage];
    }
    else {
        [self showBanner];
    }
  }

#pragma mark - View lifecycle


- (void)viewDidLoad
{
    [self.shadow setHidden:YES];    
    [self showImage];
    [self.view setHidden:NO];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
 
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:@"switchBanner"
                                                  object:nil]; 

    
    [self setMsgText:nil];
    
    [self setImageView1:nil];
    [self setImageView2:nil];
    [self setShadow:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
